﻿using System;
using System.Runtime.InteropServices;

namespace Ast.Core.Services.ObjectDefinitions
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public struct CmKeyControlBlock
    {
        public uint RefCount;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x24)]
        private byte[] _;

        public IntPtr ParentKcb;

        public IntPtr NameBlock;

        public IntPtr CachedSecurity;
    }

    /*
lkd> dt _CM_KEY_CONTROL_BLOCK -v
nt!_CM_KEY_CONTROL_BLOCK
struct _CM_KEY_CONTROL_BLOCK, 47 elements, 0xa0 bytes
    +0x000 RefCount         : Uint4B
    +0x004 ExtFlags         : Bitfield Pos 0, 16 Bits
    +0x004 PrivateAlloc     : Bitfield Pos 16, 1 Bit
    +0x004 Delete           : Bitfield Pos 17, 1 Bit
    +0x004 HiveUnloaded     : Bitfield Pos 18, 1 Bit
    +0x004 Decommissioned   : Bitfield Pos 19, 1 Bit
    +0x004 LockTablePresent : Bitfield Pos 20, 1 Bit
    +0x004 TotalLevels      : Bitfield Pos 21, 10 Bits
    +0x008 DelayedDeref     : Bitfield Pos 0, 1 Bit
    +0x008 DelayedClose     : Bitfield Pos 1, 1 Bit
    +0x008 Parking          : Bitfield Pos 2, 1 Bit
    +0x00c KeyHash          : struct _CM_KEY_HASH, 4 elements, 0x10 bytes
    +0x00c ConvKey          : Uint4B
    +0x010 NextHash         : Ptr32 to struct _CM_KEY_HASH, 4 elements, 0x10 bytes
    +0x014 KeyHive          : Ptr32 to struct _HHIVE, 42 elements, 0x3b8 bytes
    +0x018 KeyCell          : Uint4B
    +0x01c KcbPushlock      : struct _EX_PUSH_LOCK, 7 elements, 0x4 bytes
    +0x020 Owner            : Ptr32 to struct _KTHREAD, 150 elements, 0x338 bytes
    +0x020 SharedCount      : Int4B
    +0x024 SlotHint         : Uint4B
    +0x028 ParentKcb        : Ptr32 to struct _CM_KEY_CONTROL_BLOCK, 47 elements, 0xa0 bytes
    +0x02c NameBlock        : Ptr32 to struct _CM_NAME_CONTROL_BLOCK, 7 elements, 0x10 bytes
    +0x030 CachedSecurity   : Ptr32 to struct _CM_KEY_SECURITY_CACHE, 6 elements, 0x2c bytes
    +0x034 ValueCache       : struct _CACHED_CHILD_LIST, 3 elements, 0x8 bytes
    +0x03c IndexHint        : Ptr32 to struct _CM_INDEX_HINT_BLOCK, 2 elements, 0x8 bytes
    +0x03c HashKey          : Uint4B
    +0x03c SubKeyCount      : Uint4B
    +0x040 KeyBodyListHead  : struct _LIST_ENTRY, 2 elements, 0x8 bytes
    +0x040 FreeListEntry    : struct _LIST_ENTRY, 2 elements, 0x8 bytes
    +0x048 KeyBodyArray     : [4] Ptr32 to struct _CM_KEY_BODY, 10 elements, 0x2c bytes
    +0x058 KcbLastWriteTime : union _LARGE_INTEGER, 4 elements, 0x8 bytes
    +0x060 KcbMaxNameLen    : Uint2B
    +0x062 KcbMaxValueNameLen : Uint2B
    +0x064 KcbMaxValueDataLen : Uint4B
    +0x068 KcbUserFlags     : Bitfield Pos 0, 4 Bits
    +0x068 KcbVirtControlFlags : Bitfield Pos 4, 4 Bits
    +0x068 KcbDebug         : Bitfield Pos 8, 8 Bits
    +0x068 Flags            : Bitfield Pos 16, 16 Bits
    +0x06c KCBUoWListHead   : struct _LIST_ENTRY, 2 elements, 0x8 bytes
    +0x074 DelayQueueEntry  : struct _LIST_ENTRY, 2 elements, 0x8 bytes
    +0x074 Stolen           : Ptr32 to UChar
    +0x07c TransKCBOwner    : Ptr32 to struct _CM_TRANS, 12 elements, 0x68 bytes
    +0x080 KCBLock          : struct _CM_INTENT_LOCK, 2 elements, 0x8 bytes
    +0x088 KeyLock          : struct _CM_INTENT_LOCK, 2 elements, 0x8 bytes
    +0x090 TransValueCache  : struct _CHILD_LIST, 2 elements, 0x8 bytes
    +0x098 TransValueListOwner : Ptr32 to struct _CM_TRANS, 12 elements, 0x68 bytes
    +0x09c FullKCBName      : Ptr32 to struct _UNICODE_STRING, 3 elements, 0x8 bytes
    */
}